define(['jquery','api','user','util','text!pages/fund/bind.html','Vue'],function($,api,user,util,tpl,Vue){
	var vm,
	   channelId,
	   params={},
	   controller = {
	   init:function(id,backUrl){
	   	   if(!user.checkLogin())return;
	   	   	api.headerShow(1);
			api.headerTitle(L('html_title_fund_bind'));
	   	   api.footerShow(0);//隐藏底部导航
		   api.setTitle(L('html_title_fund_bind'));
		   
		   channelId=id;
		   params.backUrl = backUrl;
		   this.render();
	   },
	   render:function(){
	   	   var that=this;
	   	   $('#common').html('<div id="bind"></div>');
	            vm=new Vue({
	               el:'#bind',
	               data:{
	               	  show:true,
	               	  channelId:channelId,
	               	  imgSrc:false,
	               	  provinces:{},
	                  citys:{},
	                  banks:{},
	                  form:{
	                 	bankid:'',
	                 	channelid:channelId,
		                bankprovince:'',
		                bankcity:'',
		                bankaccount:'',
		                bankaccountname:'',
		                idcode:'',
		                bankmobile:'',
		                captcha:'',
		                verifycode:'',
		                isprimary:1,
		                upsert:1
	                 },
	                 captchaImg:''
	               },
	               watch:{
		               'form.bankprovince':function(v){
		                 	this.city='';
		                 	that.initCitys(v);
		                }
		           },
	               methods:{
	               		resetForm:function(){
				       		this.form={
				             	bankid:'',
				             	channelid:channelId,
				                bankprovince:'',
				                bankcity:'',
				                bankaccount:'',
				                bankaccountname:'',
				                idcode:'',
				                bankmobile:'',
				                captcha:'',
				                verifycode:'',
				                isprimary:1,
				                upsert:1
				            };
				       },
	               		isValid:function(data){
				       	  var error=false,
				       	      errorHash={
				       	          'verifycode':'请输入验证码！',
				       	           bankid:'请选择银行！',
				       	           province:'请选择开户省份！',
				       	           city:'请选择开户城市！',
				       	          'idcode':'请输入正确的身份证号码！',
				       	          'bankmobile':'请输入银行预留手机号！',
				       	          'bankaccount':'请输入银行卡号！',
				       	          'bankaccountname':'请输入持卡人姓名！'
				       	      };
				       	  for(var key in errorHash){
				       	  	  if (data[key]=='') {
						          error=errorHash[key];
						          break;
						      }
				       	  }
				       	  if(!error&&!util.isIdCardNo(data.idcode)){
				       	      error='请输入正确的身份证号码！';
				       	  }
						  if(error){
						  	  util.msg(error);
						      return false;
						  }else{
						      return true;
						  }
				       },
	               	  back:function(){
	               	  		setTimeout(function(){
		       	    			location.hash='#/bank/list';
		       	    		},500);
//	               	  	  var url=decodeURIComponent(params.backUrl)
//	               	  	  location.href=url;
	               	  },
	                  changeCaptcha:function(){
	                      that.loadCaptcha();
	                  },
	                  getVerify:function(){
						    if(this.form.bankmobile!=''&&this.form.captcha!=''){
						    	util.Verify.send(this.form.bankmobile,this.form.captcha,$('#btnVerify'));
						    }else if(this.form.bankmobile==''){
						        util.msg('请先输入手机号！');
						    }else if(this.form.captcha==''){
						        util.msg('请先输入验证码！');
						    }
		              },
	                  doBind:function(){
		              		var data=this.form;
		                	
		                	if(this.isValid(data)){
		                		data.bankcityname=this.citys[data.bankcity];
		                		data.bankprovincename=this.provinces[data.bankprovince];
		                		data.bankdeposit=this.banks[data.bankid];
		                		api.bind(data,function(rs){
		                		    if(rs.retcode=='0'){
		                		        util.msg('绑定成功！');
		                		        vm.resetForm();
		                		        setTimeout(function(){vm.back()},500)
		                		    }else{
		                		    	util.msg('绑定失败');
		                		    }
		                		});
		                	}
		              }
	               },
	               template:tpl
	           });
	           
	           that.loadPrivinces();
		     that.loadBanks();
		     that.loadCaptcha();
	   },
	   loadPrivinces:function(){
	       api.privinces(function(rs){
	           if(rs.retcode=='0'){
	               vm.provinces=rs.data;
	           }
	       })
	   },
	   loadBanks:function(){
	       api.banks(function(rs){
	           if(rs.retcode=='0'){
	               vm.banks=rs.data;
	           }
	       })
	   },
	   initCitys:function(v){
	       api.citys(v,function(rs){
	           if(rs.retcode=='0'){
	               vm.citys=rs.data;
	           }
	       })
	   },
	   loadCaptcha:function(){
	       api.captcha(function(rs){
	           if(rs.retcode=='0'){
	               vm.imgSrc='data:image/png;base64,'+rs.imgcode;
	           }
	       })
	   },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})